package com.axonactive.yolo.webservice.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.log4j.Logger;

public class Utils {
	private static final Logger LOGGER = Logger.getLogger(Utils.class);

	/**
	 * @param param
	 * @return string json
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static String call(String param) throws IOException,
			InterruptedException {
		URL requestUrl = new URL(CONSTANT.DEV_SERVER + param + ".html");
		URLConnection con = requestUrl.openConnection();
		BufferedReader rd = new BufferedReader(new InputStreamReader(
				con.getInputStream()));
		StringBuilder sb = new StringBuilder();
		int cp;
		try {
			while ((cp = rd.read()) != -1) {
				sb.append((char) cp);
			}
		} catch (Exception e) {
			LOGGER.error(e);
			return "error : " + e.toString();
		}
		String json = sb.toString();
		return json;
	}
	
	@SuppressWarnings("deprecation")
	public static String encode(String param){
		String temp = param;
		temp = temp.replace("/", "-");
		temp = URLEncoder.encode(temp);
		temp = temp.replace("+", "%20");
		return temp;
	}
	

//	public static Map<String, String> parse(JSONObject json,
//			Map<String, String> out) throws JSONException {
//		@SuppressWarnings("unchecked")
//		Iterator<String> keys = json.keys();
//		while (keys.hasNext()) {
//			String key = keys.next();
//			String val = null;
//			try {
//				JSONObject value = json.getJSONObject(key);
//				parse(value, out);
//			} catch (Exception e) {
//				val = json.getString(key);
//			}
//
//			if (val != null) {
//				out.put(key, val);
//			}
//		}
//		return out;
//	}
}
